package cn.com.open.opensaas.scoreservice.controller;


import cn.com.open.opensaas.scoreservice.service.QuestionContentService;
import cn.com.open.opensaas.scoreservice.util.BaseControllerUtil;
import cn.com.open.opensaas.scoreservice.util.WebUtils;
import cn.com.open.opensaas.scoreservice.controller.interceptor.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 试题判分接口
 */
@Controller
@RequestMapping("/Question/")
public class QuestionJudgeController extends BaseControllerUtil {
    private static final Logger log = LoggerFactory.getLogger(QuestionJudgeController.class);

    @Autowired
    private QuestionContentService questionContentService;

    /**
     * 试题判分
     *
     * @param request
     * @param response
     */
    @Log(optType = "试题判分", optDescription = "", keyName = "")
    @RequestMapping(value = "Judge", method = RequestMethod.POST)
    public void judge(HttpServletRequest request, HttpServletResponse response, @RequestBody Map dataObj) {
        log.info("-----------------------judge start----------------");

        //存储最终返回值
        Map<String, Object> resultMap = new LinkedHashMap<>();

        //获取参数ItemResult(学生答案Json)
        Object itemResult = dataObj.get("Items");
        request.setAttribute("Items", itemResult);

        if (StringUtils.isEmpty(itemResult)) {
            WebUtils.writeJudgeErrorMap(-201, "参数不完整", response);
        } else {
            List<Map<String, Object>> questionList = (List<Map<String, Object>>) itemResult;
            if (questionList.isEmpty()) {
                WebUtils.writeJudgeErrorMap(-201, "参数不完整", response);
            } else {
                resultMap = questionContentService.judge(questionList);
            }
        }

        WebUtils.writeJsonToMap(response, resultMap);
    }
}